import request from '../../../utils/request';
import { searchFormIf, addForm, deleteReq, editForm } from '@/services/tenantManageSer';
import {checkList}from '@/utils/utils'
export default {
  namepace: 'TenantManage',
  state: {
    pagination: {},
  },
  effects: {
    *selectBegin(payload, { call, put }) {
      const searchFormData = payload.payload;
      const data = yield call(searchFormIf, searchFormData);
      checkList(data.list);
      yield put({
        type: 'searchEnd',
        payload: {
          data: data,
          pagination: {
            page_size: data.page_size,
            total_pages: data.total_pages,
            page_num: data.page_num,
          },
          searchQ: searchFormData,
        },
      });
    },
    *addBegin({ payload, callback }, { call, put }) {
      const formData = payload.values;
      const addNow = yield call(addForm, formData);

      if (callback && typeof callback === 'function') {
        callback(addNow);
      }

      yield put({
        type: 'searchEnd',
      });
    },
    *deleteBegin({ payload, callback }, { call, put }) {
      const deleteNow = yield call(deleteReq, payload);
      if (callback && typeof callback === 'function') {
        callback(deleteNow);
      }
      yield put({
        type: 'searchEnd',
      });
    },
    *editBegin({ payload, callback }, { call, put }) {
      const editNow = yield call(editForm, payload);
      try {
        if (callback && typeof callback === 'function') {
          callback(editNow);
        }
      } catch (err) {
        console.log(err);
      }
      yield put({
        type: 'searchEnd',
      });
    },
  },
  reducers: {
    searchEnd(state, { payload: payload }) {
      return {
        dataSource: payload,
      };
    },
  },
};
